package com.yunji.base.dao.impl;


import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.yunji.base.dao.IAllianceDao;
import com.yunji.base.domain.AllianceDO;
import com.yunji.base.mapper.IAllianceMapper;
import com.yunji.base.vo.AllianceDTO;
import com.yunji.common.base.BaseDaoImpl;
import com.yunji.common.model.domain.MemberDO;
import com.yunji.common.utils.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author : peter-zhu
 * @date : 2024/12/10 14:36
 * @description : TODO
 **/
@Repository
public class AllianceDaoImpl extends BaseDaoImpl<IAllianceMapper, AllianceDO> implements IAllianceDao {

    @Override
    public List<AllianceDTO> alliancePageList(AllianceDTO alliance) {
        MPJLambdaWrapper<AllianceDO> wrapper = new MPJLambdaWrapper<>();
        return pageJoinList(AllianceDTO.class, alliance, wrapper.selectAll(AllianceDO.class)
                .selectAs(AllianceDO::getSiteCity,"city").selectAs(AllianceDO::getSiteProvince,"province")
                .selectAs(AllianceDO::getSiteDistrict,"area").selectAs(AllianceDO::getSiteAddress,"address")
                .selectAs(MemberDO::getName, "userName").selectAs(MemberDO::getPhone, "userPhone")
                .selectAs(MemberDO::getCreateTime, "applyTime")
                .leftJoin(MemberDO.class, MemberDO::getId, AllianceDO::getMemberId)
                .eq(alliance.getId() != null, AllianceDO::getId, alliance.getId())
                .eq(alliance.getAllianceStatus() != null, AllianceDO::getAllianceStatus,alliance.getAllianceStatus())
                .like(StringUtils.isNotEmpty(alliance.getUserPhone()), MemberDO::getPhone, alliance.getUserPhone())
                .like(StringUtils.isNotEmpty(alliance.getUserName()), MemberDO::getName, alliance.getUserName())
                .like(StringUtils.isNotEmpty(alliance.getPhone()), AllianceDO::getPhone, alliance.getPhone())
                .like(StringUtils.isNotEmpty(alliance.getName()), AllianceDO::getName, alliance.getName())
                .between(alliance.getStartTime() != null && alliance.getEndTime() != null
                        , AllianceDO::getEnterTime, alliance.getStartTime(), alliance.getEndTime())
                .between(alliance.getApplyStartTime() != null && alliance.getApplyEndTime() != null
                        , AllianceDO::getCreateTime, alliance.getApplyStartTime(), alliance.getApplyEndTime())
                .between(alliance.getCheckStartTime() != null && alliance.getCheckEndTime() != null
                        , AllianceDO::getCheckTime, alliance.getCheckStartTime(), alliance.getCheckEndTime())
                .like(StringUtils.isNotEmpty(alliance.getCheckName()), AllianceDO::getCheckName, alliance.getCheckName())
                .orderByDesc(AllianceDO::getCreateTime));
    }

    @Override
    public Long selectAllianceByMemberId(Long memberId) {
        return lambdaQuery().eq(AllianceDO::getMemberId, memberId).count();
    }

    @Override
    public List<String> searchTableFiled(AllianceDTO dto) {
        return this.baseMapper.searchTableFiled(dto);
    }

//    @Override
//    public Long countAlliance(AllianceCountParam param) {
//        return lambdaQuery().eq(param.getMemberId() != null, Alliance::getMemberId, param.getMemberId())
//                .eq(param.getAllianceStatus() != null, Alliance::getAllianceStatus, param.getAllianceStatus()).count();
//    }
}
